<?php

namespace wechatcom;

include "wechat.class.php";

use app\admin\model\UserPvLog;
use app\sys\model\QyeSendLog;
use think\facade\Env;
use think\facade\Log;
use Wechat;

class WechatComment
{
    private $api;

    public $users;
    public $agentId;
    public $staffNo;
    public $userNo;
    public $storeId;
    public $ToUser = array();
    public $all;
    private $data;
    private $url;
    /**
     * @var Wechat
     */
    private $weObj;

    public function __construct($appid,$appsecret)
    {
        $options = array(
            'appid'=>$appid, //填写高级调用功能的app id
            'appsecret'=>$appsecret //填写高级调用功能的密钥
        );
        $this->weObj = new Wechat($options);
    }


    public function sendMessage():string
    {
        foreach ($this->ToUser as $k => $v){
            $info = array();
            $info["touser"] = $v;
            $info["template_id"] = Env::get("WXF.APP_TMP_id");
            $info["url"] = $this->url;
            $info["data"] = $this->data;

            try {
                // 记录添加
                $res = QyeSendLog::addSendLog($this->staffNo,$this->userNo);
                if($res != "false"){
                    Log::info("QyeMessageSend".json_encode($info));
                    $r = $this->weObj->sendTemplateMessage($info);
                    if($r==false){
                        Log::info("QyeMessageSend:err".$this->weObj->errMsg);
                    }
                }
            } catch (\Exception $e) {
                Log::info("QyeMessageSend".json_encode($info)."err".$e->getMessage());
                return $e->getMessage();
            }
            return "";
        }
        return "";
    }

    public function setMessage($userArr,$all,$name,$content,$url,$agent_id="",$staff_no="",$user_no="",$store_id=0)
    {
        $this->all = $all;
        $this->ToUser = $userArr;
        $this->agentId = $agent_id;
        $this->staffNo = $staff_no;
        $this->userNo = $user_no;
        $this->storeId = $store_id;
        $this->url = $url;

        $this->getInfo($name,$content);
    }

    public function getInfo($name,$content)
    {
        // 店铺信息
        if(!$content){
            $logFind = UserPvLog::field("title")->where("pxs_id",$this->storeId)->group("id desc")->find();
            if($logFind) $content = "店铺信息".$logFind["title"];
        }

        $date = date("Y-m-d H:i:s");
        $data = array();

        $dat1 = array();
        $dat1["value"] = "客服系统实时消息";
        $dat1["color"] = "#173177";
        $data["first"] = $dat1;

        $dat12 = array();
        $dat12["value"] = "$name";
        $dat12["color"] = "#173177";
        $data["keyword1"] = $dat12;

        $dat22 = array();
        $dat22["value"] = "在线~";
        $dat22["color"] = "#173177";
        $data["keyword2"] = $dat22;

        $dat2 = array();
        $dat2["value"] = "$date";
        $dat2["color"] = "#173177";
        $data["keyword3"] = $dat2;

        $dat3 = array();
        $dat3["value"] = "$content";
        $dat3["color"] = "#173177";
        $data["remark"] = $dat3;

        $this ->data= $data;

    }

    public function getUserInfoByCode($code):array
    {
        $_GET['code'] = isset($code)?$code:'';
        $weObj = $this->weObj;
        $acc =  $weObj->getOauthAccessToken();

        if(!$acc && $weObj->errCode > 0 ){
            return ["err","微信链接错误~ {$_GET['code']} - $weObj->errCode - $weObj->errMsg"];
        }
        if(!$acc){
            return ["err","微信链接错误~ $weObj->errCode - $weObj->errMsg"];
        }
        $UserInfo = $weObj->getOauthUserinfo($acc["access_token"],$acc["openid"]);
        if(!$UserInfo && $weObj->errCode > 0){
            return ["err","微信链接错误~ $weObj->errCode - $weObj->errMsg"];
        }

        if(!$UserInfo){
            return ["err","微信链接错误~ $weObj->errCode - $weObj->errMsg"];
        }
        Log::info("getUserInfoByCode:".json_encode($UserInfo));
        return [$UserInfo["openid"],""];
    }

}